import { logPlugin } from "@babel/preset-env/lib/debug";
import axios from "axios"; // 引入axios
import { Cookie, Toast } from "../utils";
import { lawList, legalCertification } from "@/api";
import { decrypt } from "@/utils/encryption.js";
const http = axios.create({
  //设置基准地址
  baseURL: decrypt(process.env.VUE_APP_THELAW_API_HOST),
  //请求超时设置，单位ms
  timeout: 5000,
  headers: {
    moaVerifyToken: Cookie.getCookie("moaVerifyToken") || "",
  },
});
// 请求拦截器
http.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    return Promise.error(error);
  },
);
// 响应拦截器
http.interceptors.response.use(
  async (response) => {
    if (response.data.status === 200) {
      //保存用户法律咨询库的token
      if (response.request.responseURL.includes("/loginVerify")) {
        Cookie.setCookie("moaVerifyToken", response.data.moaVerifyToken);
      }
      return Promise.resolve(response);
    } else if (response.data.status === 401) {
      //认证失败时重新获取token
      //用户认证
      await legalCertification();
      location.reload(true);
    } else if (response.request.responseURL.includes("/shortPath/v2")) {
      //当接口为最短路径的查关系时
      return Promise.resolve(response.data);
    } else {
      return Promise.reject(response);
    }
  },
  (error) => {
    Toast.fail(error);
  },
);
export default http;
